package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean updateLocation(Long userId ,Double longitude, Double latitude, String addrStr) {
        try {
            //查询userlocation表有本账号地址，如果有就更新，如果没有就封装一个userlocation存如表中、
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation user = mongoTemplate.findOne(query,UserLocation.class);
            if (user == null){
                //如果没有
                UserLocation userLocation = new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
                userLocation.setAddress(addrStr);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLocation);
            }else {
                //如果有就更新
                Update update = Update.update("location",new GeoJsonPoint(longitude,latitude))
                        .set("updated",System.currentTimeMillis())
                        .set("lastUpdated",user.getUpdated());
                mongoTemplate.updateFirst(query,update,UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Long> queryNearUser(Long userId, Double valueOf) {
        //查询滋生的地理位置
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if (userLocation == null) {
            return null;
        }
        //定位自身地理位置
        GeoJsonPoint point = userLocation.getLocation();
        //定位半径
        Distance distance = new Distance(valueOf / 1000 , Metrics.KILOMETERS);
        //把圆形画出来
        Circle circle = new Circle(point, distance);
        //查询
        Query locationQuery = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(locationQuery, UserLocation.class);
        List<Long> ids = CollUtil.getFieldValues(userLocations, "userId", Long.class);
        return ids;
    }
}
